<template>
	<view>
		<u-popup :show="changeManVisible" @close="closeFunc" mode="bottom" closeable round="10">
			<view class="fp-box-title">人员修改</view>
			<view class="fp-box">
				<u--form labelPosition="top" :model="changeManForm" labelWidth="auto">
					<u-form-item label="职位">
						<u--input customStyle="padding:20rpx;" v-model="typeName" disabled border="none" placeholder="选择客户经理"></u--input>
					</u-form-item>
					<u-form-item required label="客户经理" borderBottom v-if="changeManForm.type == 1" @click="chooseManFunc">
						<u--input
							customStyle="padding:10rpx;"
							v-model="changeManForm.admin_name"
							readonly
							border="none"
							placeholder="选择客户经理"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
					<u-form-item required label="设计师" borderBottom v-if="changeManForm.type == 2" @click="chooseManFunc">
						<u--input
							customStyle="padding:10rpx;"
							v-model="changeManForm.admin_name"
							readonly
							border="none"
							placeholder="选择设计师"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
					<u-form-item required label="造价员" borderBottom v-if="changeManForm.type == 3" @click="chooseManFunc">
						<u--input
							customStyle="padding:10rpx;"
							v-model="changeManForm.admin_name"
							readonly
							border="none"
							placeholder="选择造价员"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
					<u-form-item required label="施工工程经理" borderBottom v-if="changeManForm.type == 4 || changeManForm.type == 7" @click="chooseManFunc">
						<u--input
							customStyle="padding:10rpx;"
							v-model="changeManForm.admin_name"
							readonly
							border="none"
							placeholder="选择施工工程经理"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
					<u-form-item required label="工程监理" borderBottom v-if="changeManForm.type == 7" @click="chooseManFunc('engineering_supervision')">
						<u--input
							customStyle="padding:10rpx;"
							v-model="changeManForm.engineering_supervision_name"
							readonly
							border="none"
							placeholder="选择工程监理"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
					<u-form-item required label="勘场工程经理" borderBottom v-if="changeManForm.type == 5" @click="chooseManFunc">
						<u--input
							customStyle="padding:10rpx;"
							v-model="changeManForm.admin_name"
							readonly
							border="none"
							placeholder="选择勘场工程经理"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
				</u--form>
				<u-button
					:disabled="submitDisabled"
					type="primary"
					text="提交"
					color="#3B75FF"
					:customStyle="{ marginTop: '30rpx' }"
					@click="submitFunc"
				></u-button>
			</view>
		</u-popup>
		<!-- 人员选择 -->
		<u-picker
			ref="changManShow"
			title="人员选择"
			:show="changManShow"
			:columns="[changManToManOption]"
			keyName="name"
			@close="changManCloseFunc"
			@cancel="changManCloseFunc"
			@confirm="changManConfirm"
			closeOnClickOverlay
		></u-picker>
		<!-- 监理列表 -->
		<u-picker
			ref="changManShow2"
			title="人员选择"
			:show="changManShow2"
			:columns="[jLmanOption]"
			keyName="name"
			@close="changManCloseFunc"
			@cancel="changManCloseFunc"
			@confirm="changManConfirm"
			closeOnClickOverlay
		></u-picker>
	</view>
</template>

<script>
export default {
	name: 'changeManCom',
	props: {
		show: {
			type: Boolean,
			default: false
		},
		type: {
			type: Number,
			default: 1
		},
		projectId: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			changManShow2: false, //工程监理
			changManShow: false, //人员选择
			changeManVisible: false, //显示/隐藏
			changeManForm: {
				type: '',
				admin_id: '',
				admin_name: '',
				engineering_supervision_id: '',
				engineering_supervision_name: ''
			}, //
			changeManTypeList: [
				{
					label: '客户经理',
					value: 1
				},
				{
					label: '设计师',
					value: 2
				},
				{
					label: '造价员',
					value: 3
				},
				{
					label: '勘场工程经理',
					value: 4
				},
				{
					label: '施工工程经理',
					value: 7
				},
				{
					label: '勘场工程经理',
					value: 5
				}
			],
			changManToManOption: [], //人员列表
			changeManProjectObj: '', //项目基本数据
			jLmanOption: [], //监理列表
			admin_id: '',
			engineering_supervision_id: '',
			typeName: '',
			projectObj: '', //项目信息
			jlType: '' //监理选择的判断  有值是监理
		};
	},
	watch: {
		show(value) {
			this.changeManVisible = value;
			this.changeManForm.type = this.type;
			let index = this.changeManTypeList.findIndex((item) => {
				return item.value == this.type;
			});
			this.typeName = this.changeManTypeList[index].label;
			this.$nextTick(() => {
				this.selectProjectDetails(this.projectId);
			});
		}
	},
	computed: {
		submitDisabled() {
			if (this.changeManForm.type == 7) {
				return this.admin_id == this.changeManForm.admin_id && this.changeManForm.engineering_supervision_id == this.engineering_supervision_id;
			} else {
				return this.admin_id == this.changeManForm.admin_id;
			}
		}
	},
	methods: {
		// 关闭弹窗
		closeFunc() {
			this.$emit('closeFunc', false);
			this.changeManForm = {
				type: '',
				admin_id: '',
				admin_name: '',
				engineering_supervision_id: '',
				engineering_supervision_name: ''
			};
		},
		// 获取项目基本信息
		selectProjectDetails(project_id) {
			if (!this.projectId) return;
			this.$Apipc
				.selectProjectDetails({
					project_id
				})
				.then((res) => {
					console.log(res, '获取项目基本信息');
					if (res.code == 200) {
						this.projectObj = res.data;
						this.engineering_supervision_id = res.data.engineering_supervision_id ? JSON.parse(JSON.stringify(res.data.engineering_supervision_id)) : '';
						this.changeManForm.engineering_supervision_id = res.data.engineering_supervision_id ? JSON.parse(JSON.stringify(res.data.engineering_supervision_id)) : '';
						this.changeManForm.engineering_supervision_name = res.data.engineering_supervision_name
							? JSON.parse(JSON.stringify(res.data.engineering_supervision_name))
							: '';
						this.changeManTypeFunc(res.data);
					}
				});
		},
		// 修改人员类型选择
		changeManTypeFunc(projectObj) {
			let { type } = this.changeManForm;
			console.log(projectObj, '项目数据');
			switch (type) {
				case 1:
					this.changManToManList(2, projectObj.account_manager_id || ''); //客户经理
					break;
				case 2:
					this.changManToManList(3, projectObj.designer_id || ''); //设计师
					break;
				case 3:
					this.changManToManList(5, projectObj.cost_estimator_id || ''); //造价员
					break;
				case 4:
					this.changManToManList(7, projectObj.site_surveyors_id || ''); //勘场工程经理
				case 7:
					this.changManToManList(7, projectObj.engineering_manager_id || ''); //施工工程经理
					this.changManToManList(7, projectObj.engineering_supervision_id || '', type); //工程监理
					break;
				case 5:
					this.changManToManList(7, projectObj.site_surveyors_id || ''); //勘场工程经理
					break;
				default:
			}
			this.changeManProjectObj = projectObj;
		},
		// 修改人员（请求对应的职位列表）  type存在请求工程监理列表
		changManToManList(position_id_arr, admin_id, type) {
			this.changeLoading = true;
			this.$Apipc
				.getAdminList({
					page: 1,
					num: 999,
					identity: type ? 1 : position_id_arr == 7 ? 2 : 1,
					position_id_arr: type ? [8] : [position_id_arr]
				})
				.then((res) => {
					console.log(res, '修改人员（请求对应的职位列表）');
					this.changeLoading = false;
					if (res.code == 200) {
						if (type) {
							this.jLmanOption = res.data.list;
							return;
						}
						let obj = res.data.list.find((item) => {
							return item.id == admin_id;
						});

						this.changManToManOption = res.data.list;
						let adminIndex = res.data.list.findIndex((item) => {
							return item.id == admin_id;
						});
						this.$nextTick(() => {
							this.admin_id = JSON.parse(JSON.stringify(admin_id));
							this.changeManForm.admin_id = JSON.parse(JSON.stringify(admin_id));
							this.changeManForm.admin_name = adminIndex != -1 ? res.data.list[adminIndex].name : '';
						});
					}
				});
		},
		// 开启人员选择弹窗
		chooseManFunc(type) {
			let changManToManOption = this.changManToManOption;
			let projectObj = this.projectObj;
			let changeManForm = this.changeManForm;
			let index = changManToManOption.findIndex((item) => {
				return changeManForm.admin_id == item.id;
			});
			switch (this.type) {
				case 1:
					//客户经理
					this.$refs.changManShow.setIndexs([index == -1 ? 0 : index], true);
					break;
				case 2:
					//设计师
					this.$refs.changManShow.setIndexs([index == -1 ? 0 : index], true);
					break;
				case 3:
					//造价员
					this.$refs.changManShow.setIndexs([index == -1 ? 0 : index], true);
					break;
				case 4:
					//勘场工程经理
					this.$refs.changManShow.setIndexs([index == -1 ? 0 : index], true);
					break;
				case 7:
					//工程监理
					if (type) {
						this.jlType = type;
						this.changManShow2 = true;
						this.changeManVisible = false;
						let index2 = this.jLmanOption.findIndex((item) => {
							return item.id == changeManForm.engineering_supervision_id;
						});
						this.$refs.changManShow2.setIndexs([index2 == -1 ? 0 : index2], true);
						return;
					} else {
						// 工程经理
						this.$refs.changManShow.setIndexs([index == -1 ? 0 : index], true);
					}
					break;
				case 5:
					//勘场工程经理
					this.$refs.changManShow.setIndexs([index == -1 ? 0 : index], true);
					break;
				default:
			}
			this.changManShow = true;
			this.jlType = '';
			this.changeManVisible = false;
		},
		// 关闭人员选择弹窗
		changManCloseFunc() {
			this.changManShow = false;
			this.changManShow2 = false;
			this.changeManVisible = true;
		},
		// 人员选择弹窗确认
		changManConfirm(e) {
			console.log(e);
			if (this.jlType) {
				this.changeManForm.engineering_supervision_id = e.value[0].id;
				this.changeManForm.engineering_supervision_name = e.value[0].name;
			} else {
				this.changeManForm.admin_id = e.value[0].id;
				this.changeManForm.admin_name = e.value[0].name;
				this.changeManForm.engineering_supervision_id = e.value[0].engineering_supervision_id || '';
				this.changeManForm.engineering_supervision_name = e.value[0].engineering_supervision_name || '';
			}
			this.changManCloseFunc();
		},
		// 确认修改人员
		submitFunc() {
			let changeManForm = this.changeManForm;
			console.log(changeManForm);
			if (!changeManForm.type) {
				this.$message({
					type: 'warning',
					message: '请先选择需要分配的职位'
				});
				return;
			}
			changeManForm.type = changeManForm.type == 7 ? 4 : changeManForm.type;
			this.updProjectLeader(changeManForm);
		},
		// 修改人员请求
		updProjectLeader(obj) {
			uni.showLoading({
				mask: true
			});
			obj.project_id_arr = [this.projectId]; //项目id
			this.$Apipc.updProjectLeader(obj).then((res) => {
				console.log(res, '修改人员');
				uni.hideLoading();
				if (res.code == 200) {
					this.$emit('changeManSuccessFunc', true);
				}
				uni.showToast({
					title: res.message,
					icon: res.code == 200 ? 'success' : 'error'
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.fp-box-title {
	height: 46px;
	border-bottom: 2rpx solid #efefef;
	text-align: center;
	line-height: 46px;
	font-size: 30rpx;
}
.fp-box {
	width: 100%;
	padding: 20rpx 30rpx 20rpx;
	box-sizing: border-box;
}
</style>
